import { reactive, ref } from 'vue';
import { defineStore } from 'pinia';
import * as SystemUserApi from '../api/SystemUserApi';
import { UserInfo } from '../domain/entity/UserInfo';

export const useUserInfoStore = defineStore('userInfoManager', () => {
    const userInfo = reactive<UserInfo>(new UserInfo());
    function updateUserInfo() {
        // 请求地址获取数据
        SystemUserApi.getUserInfo().then(response => response.data.data).then(data => {
            userInfo.id = data.id;
            userInfo.nickname = data.nickname;
            userInfo.username = data.username;
        })
    }

    function getUserInfo() {
        if (userInfo.username === undefined || userInfo.username === null) {
            updateUserInfo();
        }
        return userInfo;
    }

    function removeUserInfo() {
        userInfo.id = undefined;
        userInfo.nickname = undefined;
        userInfo.username = undefined;
    }

    return { userInfo, updateUserInfo, getUserInfo, removeUserInfo }
}, {
    persist: true,
},);


